package com.iisigroup.streetblock.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.iisigroup.streetblock.common.app.GenericApp;
import com.iisigroup.streetblock.service.StreetBlockService;
import com.iisigroup.streetblock.vo.StreetBlockVO;

/**
 * 街廓的主程式
 * @author Ebi
 * @since 2015/01/13
 */
public class StreetBlockApp extends GenericApp {
	
	private static final String FILENAME_NEIGHBORHOOD = "彰化縣街廓(鄰).txt";
	private static final String FILENAME_VILLAGE = "彰化縣街廓(村里).txt";
	private static final String FILENAME_CITY = "彰化縣街廓(鄉鎮市區).txt";
	private static final String ENCODING = "BIG5";
	
	@Autowired
	private StreetBlockService streetBlockService;
	
	public StreetBlockApp() {
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
	    context.getAutowireCapableBeanFactory().autowireBean(this);
	    context.close();
	}
	
	public static void main(String[] args) {
		String fileName = args[0];
		String url = "./" + fileName;
		StreetBlockApp streetBlock = new StreetBlockApp();
		streetBlock.readFile(url);
	}
	
	/**
	 * 讀取檔案
	 * @param url 檔案位置
	 */
	private void readFile(String url) {
		log.debug("開始分析明細檔...");
		List<StreetBlockVO> list = streetBlockService.readFile(url, StreetBlockApp.ENCODING);
		log.debug("...分析完成");
		log.debug("開始輸出彰化縣街廓(鄰)參考檔...");
		streetBlockService.output(list, StreetBlockApp.FILENAME_NEIGHBORHOOD, StreetBlockApp.ENCODING, StreetBlockVO.TYPE_NEIGHBORHOOD, false);
		log.debug("...完成");
		log.debug("開始輸出彰化縣街廓(村里)參考檔...");
		streetBlockService.output(list, StreetBlockApp.FILENAME_VILLAGE, StreetBlockApp.ENCODING, StreetBlockVO.TYPE_VILLAGE, false);
		log.debug("...完成");
		log.debug("開始輸出彰化縣街廓(鄉鎮市區)參考檔...");
		streetBlockService.output(list, StreetBlockApp.FILENAME_CITY, StreetBlockApp.ENCODING, StreetBlockVO.TYPE_CITY, false);
		log.debug("...完成");
		log.debug("輸出完成");
	}
}
